package com.jdd.modules.mqtt.utils;

import com.alibaba.fastjson.JSONObject;
import com.jdd.modules.mqtt.constant.MqttCmdConstant;
import com.jdd.common.entity.MqttUser;

/**
 * @className MqttCmdUtill
 * @author: zxl
 * @describe: mqtt指令集合
 * @date: 2022/9/27/19:14
 * @vision: 1.0
 */
public class MqttCmdUtill {
    /**
     * 设别开门
     *
     * @param client_id 客户端id，其实就是设备sn
     * @param cmd_id     信息的标识id，其实就是发送表记录的id
     * @return com.alibaba.fastjson.JSONObject
     * @throws
     * @author zxl
     * @date 2022/09/28 16:08
     */
    public static JSONObject openDoor(String client_id, String cmd_id) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(MqttCmdConstant.DoorController.CTRL_TYPE, "on");
        jsonObject.put(MqttCmdConstant.CMD, MqttCmdConstant.DoorController.OPEN_DOOR);
        jsonObject.put(MqttCmdConstant.CLIENT_ID, client_id);
        jsonObject.put(MqttCmdConstant.CMD_ID, cmd_id);
        return jsonObject;
    }

    public static JSONObject openDoor(String client_id) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(MqttCmdConstant.DoorController.CTRL_TYPE, "on");
        jsonObject.put(MqttCmdConstant.CMD, MqttCmdConstant.DoorController.OPEN_DOOR);
        jsonObject.put(MqttCmdConstant.CLIENT_ID, client_id);
        jsonObject.put(MqttCmdConstant.CMD_ID, "");
        return jsonObject;
    }
    /**
     * 设别关门
     *
     * @param client_id 客户端id，其实就是设备sn
     * @param cmd_id     信息的标识id，其实就是发送表记录的id
     * @return com.alibaba.fastjson.JSONObject
     * @throws
     * @author zxl
     * @date 2022/09/28 16:08
     */
    public static JSONObject closeDoor(String client_id, String cmd_id) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(MqttCmdConstant.DoorController.CTRL_TYPE, "off");
        jsonObject.put(MqttCmdConstant.CMD, MqttCmdConstant.DoorController.OPEN_DOOR);
        jsonObject.put(MqttCmdConstant.CLIENT_ID, client_id);
        jsonObject.put(MqttCmdConstant.CMD_ID, cmd_id);
        return jsonObject;
    }
    /**
     * 根据per_id查询人员
     *
     * @param client_id 客户端id，其实就是设备sn
     * @param cmd_id     信息的标识id，其实就是发送表记录的id
     * @param per_id    用户在设备上的id。其实就是user表用户id
     * @return com.alibaba.fastjson.JSONObject
     * @throws
     * @author zxl
     * @date 2022/09/28 16:06
     */
    public static JSONObject queryFaceByPerId(String client_id, String cmd_id, String per_id) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(MqttCmdConstant.CMD, MqttCmdConstant.PeopleController.Query.QUERY_FACE);
        jsonObject.put(MqttCmdConstant.PeopleController.Query.PER_ID, per_id);
        jsonObject.put(MqttCmdConstant.CLIENT_ID, client_id);
        jsonObject.put(MqttCmdConstant.CMD_ID, cmd_id);
        return jsonObject;
    }

    /**
     * 根据下标查询人员信息
     * 总下标(页数*页大小 + 第几条)，0 - n
     *
     * @param client_id 客户端id，其实就是设备sn
     * @param cmd_id     信息的标识id，其实就是发送表记录的id
     * @param id        用户在设备里的下标
     * @return com.alibaba.fastjson.JSONObject
     * @throws
     * @author zxl
     * @date 2022/09/28 16:07
     */
    public static JSONObject queryFaceById(String client_id, String cmd_id, String id) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(MqttCmdConstant.CMD, MqttCmdConstant.PeopleController.Query.QUERY_FACE);
        jsonObject.put(MqttCmdConstant.PeopleController.Query.ID, id);
        jsonObject.put(MqttCmdConstant.CLIENT_ID, client_id);
        jsonObject.put(MqttCmdConstant.CMD_ID, cmd_id);
        return jsonObject;
    }

    /**
     * 根据名字查询人员信息，名字可能重复
     * 所以返回的不仅有第一个相同名字的信息，还有其他同名的per_id
     *
     * @param client_id 客户端id，其实就是设备sn
     * @param cmd_id     信息的标识id，其实就是发送表记录的id
     * @param name      用户的名字
     * @return com.alibaba.fastjson.JSONObject
     * @throws
     * @author zxl
     * @date 2022/09/28 16:09
     */
    public static JSONObject queryFaceByName(String client_id, String cmd_id, String name) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(MqttCmdConstant.CMD, MqttCmdConstant.PeopleController.Query.QUERY_FACE);
        jsonObject.put(MqttCmdConstant.PeopleController.Query.NAME, name);
        jsonObject.put(MqttCmdConstant.CLIENT_ID, client_id);
        jsonObject.put(MqttCmdConstant.CMD_ID, cmd_id);
        return jsonObject;
    }

    /**
     * 创建人脸 (通过url创建)
     *
     * @param client_id 客户端id，其实就是设备sn
     * @param cmd_id     信息的标识id，其实就是发送表记录的id
     * @param mqttUser   创建需要用到的信息，需要自己封装
     * @return com.alibaba.fastjson.JSONObject
     * @throws
     * @author zxl
     * @date 2022/09/28 17:14
     */
    public static JSONObject createFaceByUrl(String client_id, String cmd_id, MqttUser mqttUser) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(MqttCmdConstant.CMD, MqttCmdConstant.PeopleController.Create.UPDETE_FACE_EX);
        jsonObject.put(MqttCmdConstant.PeopleController.Create.E_TIME, mqttUser.getETime());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.FACE_ID, mqttUser.getFaceId());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.IDCAEDNUM, mqttUser.getIdcardNum());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.IMG_URL, mqttUser.getImgUrl());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.PER_id, mqttUser.getPerId());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.PER_NAME, mqttUser.getPerName());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.PER_TYPE, mqttUser.getPerType());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.USER_TYPE, mqttUser.getUsrType());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.IDCAEDPER, mqttUser.getIdcardper());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.S_TIME, mqttUser.getSTime());
        jsonObject.put(MqttCmdConstant.CLIENT_ID, client_id);
        jsonObject.put(MqttCmdConstant.CMD_ID, cmd_id);
        return jsonObject;
    }

    /**
     * 创建人脸 (通过图片base64信息创建)
     *
     * @param client_id 客户端id，其实就是设备sn
     * @param cmd_id    信息的标识id，其实就是发送表记录的id
     * @param mqttUser  创建需要用到的信息，需要自己封装
     * @return com.alibaba.fastjson.JSONObject
     * @throws
     * @author zxl
     * @date 2022/09/28 17:14
     */
    public static JSONObject createFaceByBase(String client_id, String cmd_id, MqttUser mqttUser) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(MqttCmdConstant.CMD, MqttCmdConstant.PeopleController.Create.CREATE_FACE);
        jsonObject.put(MqttCmdConstant.PeopleController.Create.E_TIME, mqttUser.getETime());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.FACE_ID, mqttUser.getFaceId());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.IDCAEDNUM, mqttUser.getIdcardNum());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.IMG_DATE, mqttUser.getImgData());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.PER_id, mqttUser.getPerId());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.PER_NAME, mqttUser.getPerName());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.PER_TYPE, mqttUser.getPerType());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.USER_TYPE, mqttUser.getPerType());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.IDCAEDPER, mqttUser.getIdcardper());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.S_TIME, mqttUser.getSTime());
        jsonObject.put(MqttCmdConstant.CLIENT_ID, client_id);
        jsonObject.put(MqttCmdConstant.CMD_ID, cmd_id);
        return jsonObject;
    }

    /**
     * 修改人脸信息
     *
     * @param client_id 客户端id，其实就是设备sn
     * @param cmd_id    信息的标识id，其实就是发送表记录的id
     * @param mqttUser  修改需要用到的信息，需要自己封装
     * @return com.alibaba.fastjson.JSONObject
     * @throws
     * @author zxl
     * @date 2022/09/28 18:44
     */
    public static JSONObject alterFace(String client_id, String cmd_id, MqttUser mqttUser) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(MqttCmdConstant.CMD, MqttCmdConstant.PeopleController.Update.UPDATE_FACE);
        jsonObject.put(MqttCmdConstant.PeopleController.Create.E_TIME, mqttUser.getETime());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.FACE_ID, mqttUser.getFaceId());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.IDCAEDNUM, mqttUser.getIdcardNum());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.IMG_URL, mqttUser.getImgUrl());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.PER_id, mqttUser.getPerId());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.PER_NAME, mqttUser.getPerName());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.PER_TYPE, mqttUser.getPerType());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.USER_TYPE, mqttUser.getPerType());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.IDCAEDPER, mqttUser.getIdcardper());
        jsonObject.put(MqttCmdConstant.PeopleController.Create.S_TIME, mqttUser.getSTime());
        jsonObject.put(MqttCmdConstant.CLIENT_ID, client_id);
        jsonObject.put(MqttCmdConstant.CMD_ID, cmd_id);
        return jsonObject;
    }

    /**
     * 删除人脸信息
     * type取值如下
     * 1 删除指定 ID 人员
     * 2 删除白名单人员
     * 3 删除黑名单人员
     * 4 删除普通名单人员
     * 5 删除所有人员
     *
     * @param client_id 客户端id，其实就是设备sn
     * @param cmd_id    信息的标识id，其实就是发送表记录的id
     * @param per_id    用户在设备上的id。其实就是user表用户id
     * @param type      删除的类型，具体参照mqtt常量类里面的，可以不写，默认为0
     * @return com.alibaba.fastjson.JSONObject
     * @throws
     * @author zxl
     * @date 2022/09/28 18:53
     */
    public static JSONObject deleteFaceByPerId(String client_id, String cmd_id, String per_id, int type) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(MqttCmdConstant.CMD, MqttCmdConstant.PeopleController.Delete.DELETE_FACE);
        jsonObject.put(MqttCmdConstant.PeopleController.Delete.PER_id, per_id);
        if (type == 1) {
            jsonObject.put(MqttCmdConstant.PeopleController.Delete.TYPE, MqttCmdConstant.PeopleController.Delete.DEL_PERSON_MODE.DEL_PERSON_ID.getType());
        } else if (type == 2) {
            jsonObject.put(MqttCmdConstant.PeopleController.Delete.TYPE, MqttCmdConstant.PeopleController.Delete.DEL_PERSON_MODE.DEL_PERSON_WHITE.getType());
        } else if (type == 3) {
            jsonObject.put(MqttCmdConstant.PeopleController.Delete.TYPE, MqttCmdConstant.PeopleController.Delete.DEL_PERSON_MODE.DEL_PERSON_BLACK.getType());
        } else if (type == 4) {
            jsonObject.put(MqttCmdConstant.PeopleController.Delete.TYPE, MqttCmdConstant.PeopleController.Delete.DEL_PERSON_MODE.DEL_PERSON_COMM.getType());
        } else if (type == 5) {
            jsonObject.put(MqttCmdConstant.PeopleController.Delete.TYPE, MqttCmdConstant.PeopleController.Delete.DEL_PERSON_MODE.DEL_PERSON_ALL.getType());
        }

        jsonObject.put(MqttCmdConstant.CLIENT_ID, client_id);
        jsonObject.put(MqttCmdConstant.CMD_ID, cmd_id);
        return jsonObject;
    }


    /**
     * type 是播放的文件类型
     * //    type=1    通过提示音 success.wav
     * //    type=2   失败提示音 failure.wav
     * //    type=3    请重刷身份证 reswipeidcard.wav
     * //    type=4    权限已过期 deadline.wav
     * //   type=5      开机语音 welcome.wav
     * //   type=6      未带安全帽禁止通行 hatpass.wav
     * //   type=7      未在有效时段 outtime.wav
     *
     * @param client_id 客户端id，其实就是设备sn
     * @param cmd_id    信息的标识id，其实就是发送表记录的id
     * @param type      播放的文件类型
     * @return com.alibaba.fastjson.JSONObject
     * @throws
     * @author zxl
     * @date 2022/09/29 16:38
     */
    public static JSONObject playVoice(String client_id, String cmd_id, int type) {
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject.put(MqttCmdConstant.CMD, MqttCmdConstant.VoiceController.TRYLISTEN_WAV);
        jsonObject.put(MqttCmdConstant.VoiceController.BODY, jsonObject1);
        jsonObject.put(MqttCmdConstant.CLIENT_ID, client_id);
        jsonObject.put(MqttCmdConstant.CMD_ID, cmd_id);
        if (type == 1) {
            jsonObject1.put(MqttCmdConstant.VoiceController.FILE_NAME, MqttCmdConstant.VoiceController.SUCCESS);
        } else if (type == 2) {
            jsonObject1.put(MqttCmdConstant.VoiceController.FILE_NAME, MqttCmdConstant.VoiceController.FAILURE);
        } else if (type == 3) {
            jsonObject1.put(MqttCmdConstant.VoiceController.FILE_NAME, MqttCmdConstant.VoiceController.RESWIPEIDVAED);
        } else if (type == 4) {
            jsonObject1.put(MqttCmdConstant.VoiceController.FILE_NAME, MqttCmdConstant.VoiceController.DEALINE);
        } else if (type == 5) {
            jsonObject1.put(MqttCmdConstant.VoiceController.FILE_NAME, MqttCmdConstant.VoiceController.WELCOME);
        } else if (type == 6) {
            jsonObject1.put(MqttCmdConstant.VoiceController.FILE_NAME, MqttCmdConstant.VoiceController.HATPASS);
        } else if (type == 6) {
            jsonObject1.put(MqttCmdConstant.VoiceController.FILE_NAME, MqttCmdConstant.VoiceController.OUTTIME);
        }
        return jsonObject;

    }

    /**
     * 创建人脸信息错误码信息
     * @author zxl
    * @param code 状态码
     * @date 2022/10/13 18:38
     * @return java.lang.String
     * @throws
     */
    public static String createUserErrorCode(int code) {
        String errorInfo = null;
        if (code == 0) {
            errorInfo = "创建成功";
        } else if (code == -1) {
            errorInfo = "未知错误";
        } else if (code == -2) {
            errorInfo = "图像预处理失败";
        } else if (code == -3) {
            errorInfo = "人脸检测出错";
        } else if (code == -4) {
            errorInfo = "未检测到人脸";
        } else if (code == -5) {
            errorInfo = "检测到人脸数大于1";
        } else if (code == -6) {
            errorInfo = "未通过人脸质量筛选";
        } else if (code == -7) {
            errorInfo = "提取特征失败";
        } else if (code == -8) {
            errorInfo = "特写图像JPEG数据缓存区过小";
        } else if (code == -9) {
            errorInfo = "编码JPEG失败";
        } else if (code == 402) {
            errorInfo = "设备已存在该人脸信息";
        } else if (code == 403) {
            errorInfo = "录入人脸的name非uft8编码";
        } else if (code == 404) {
            errorInfo = "录入人脸失败";
        } else if (code == 406) {
            errorInfo = "设备人脸录入数量已达上限";
        } else if (code == 407) {
            errorInfo = "人脸像素太大（大于1072）";
        } else if (code == 408) {
            errorInfo = "图片格式错误";
        } else if (code == 410) {
            errorInfo = "获取到的特征数据为空";
        } else if (code == 411) {
            errorInfo = "下发协议数据格式错误（姓名或id不为string）";
        } else if (code == 422) {
            errorInfo = "图片大于1MB";
        } else if (code == 423) {
            errorInfo = "图片服务器异常";
        } else if (code == 424) {
            errorInfo = "没有字符串per_id";
        } else if (code == 425) {
            errorInfo = "没有字符串img_url";
        }

        return errorInfo;
    }


    /**
     * 删除人脸信息错误码信息
     * @author zxl
    * @param code 状态码
     * @date 2022/10/13 18:37
     * @return java.lang.String
     * @throws
     */
    public static String deleteUserErrorInfo(int code) {
        String errorInfo = null;
        if (code == 405) {
            errorInfo = "设备不存在该人脸";
        } else if (code == 406) {
            errorInfo = "清除人脸失败";
        } else if (code == 407) {
            errorInfo = "下发'type'错误";
        }


        return errorInfo;
    }
}
